package com.example.esaclient;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.facebook.Session;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;

public class FBLoginActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fblogin);
		
		 // print out the key hash -- FOR DEBUG
		/*    try {
		        PackageInfo info = getPackageManager().getPackageInfo(
		                "com.example.esaclient", 
		                PackageManager.GET_SIGNATURES);
		        for (Signature signature : info.signatures) {
		            MessageDigest md = MessageDigest.getInstance("SHA");
		            md.update(signature.toByteArray());
		            Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
		            }
		    } catch (NameNotFoundException e) {

		    } catch (NoSuchAlgorithmException e) {

		    }
			
		 // start Facebook Login
	    Session.openActiveSession(this, true, new Session.StatusCallback() {

	      // callback when session changes state
	      @Override
	      public void call(Session session, SessionState state, Exception exception) {
	        if (session.isOpened()) {

	        	Facebook fb =new Facebook("336794829783831");
	        	fb.authorize(FBLoginActivity.this, new String[] { "email", "read_stream" }, new LoginDialogListener());
	        	 Log.d( "APP TOKEN =>" ,session.getAccessToken());
	        	 Toast.makeText(getApplicationContext(), 	 "Your Access token is: "+ session.getAccessToken(), Toast.LENGTH_LONG).show();
	        final  String token =  session.getAccessToken();
	          // make request to the /me API
	          Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

	            // callback after Graph API response with user object
	            @Override
	            public void onCompleted(GraphUser user, Response response) {
	              if (user != null) {
	                TextView welcome = (TextView) findViewById(R.id.welcomeFB);
	                
	                welcome.setText("Hello " + user.getName() + "!");
	                int  esaUsrID= new GlobalSettings(FBLoginActivity.this).getPrefInt(GlobalSettings.PREF_ESA_USRID);
	                new InsertToken(FBLoginActivity.this).execute(esaUsrID,token , "Facebook", profilePic);
	              }
	            }
	          });
	        }
	      }
	    });
*/
	
}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
	  super.onActivityResult(requestCode, resultCode, data);
	  Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
	  
	}

	
	 /*
     * Callback when user has authorized the app with the new permissions
     */
    private final class LoginDialogListener implements DialogListener {
        @Override
        public void onComplete(Bundle values) {
           
           Log.e("Permission->", "OK!");
        }

        @Override
        public void onFacebookError(FacebookError error) {
        	 Log.e("Permission->", "KO: "+ error.getMessage());
        }

        @Override
        public void onError(DialogError error) {
        	Log.e("Permission->", "KO: "+ error.getMessage());
        }

        @Override
        public void onCancel() {
            
        }
    }
	
}
